<?php

namespace app\admin\model;

use think\Model;

/**
 * Appfuns
 */
class Appfuns extends Model
{
    // 表名
    protected $name = 'appfuns';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    public $append=[
        'domain_image'
    ];

    public function setFastinputAttr($value,$data)
    {
        return json_encode($value,256);
    }
    public function getFastinputAttr($value,$data)
    {
        if(empty($value)){
            return [];
        }
        return json_decode($value,true);
    }

    public function getDomainImageAttr($value,$data)
    {
        if(isset($data['image'])&&!empty($data['image'])){
            return request()->domain().$data['image'];
        }
        return  '';
    }
    protected static function onAfterInsert($model): void
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getEditorAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }
}